package refactoring.rental.p3;

import java.util.List;
import java.util.stream.Collectors;

public class Report {
    public static String txtReport(Customer customer) {
        String result = "Rental Record for " + customer.getName() + "\n";
        for (Rental each : customer.getRentals()) {
            double thisAmount = each.getAmount();
            result += "\t" + each.getMovie().getTitle() + "\t" + String.format("%.2f", thisAmount) + "\n";
        }
        double totalAmount = customer.getTotalAmount();
        int frequentRenterPoints = customer.getTotalFrequentRenterPoints();
        result += "Amount owned is " + String.format("%.2f", totalAmount) + "\n";
        result += "You earned " + frequentRenterPoints + " frequent renter points";
        return result;
    }

    public static String htmlReport(Customer customer) {
        StringBuilder sb = new StringBuilder();
        sb.append("<h1>Rental Record for ").append(customer.getName()).append("</h1>\n");
        sb.append("<table>\n");
        sb.append("<tr><th>Movie</th><th>Amount</th></tr>\n");
        
        for (Rental each : customer.getRentals()) {
            sb.append("<tr><td>").append(each.getMovie().getTitle())
              .append("</td><td>").append(String.format("%.2f", each.getAmount()))
              .append("</td></tr>\n");
        }
        
        sb.append("</table>\n");
        sb.append("<p>Amount owned is <em>")
          .append(String.format("%.2f", customer.getTotalAmount()))
          .append("</em></p>\n");
        sb.append("<p>You earned <em>")
          .append(customer.getTotalFrequentRenterPoints())
          .append("</em> frequent renter points</p>");
        
        return sb.toString();
    }

    public static String jsonReport(Customer customer) {
        List<String> rentalsJson = customer.getRentals().stream()
            .map(r -> String.format(
                "{\"movie\":\"%s\",\"days\":%d,\"amount\":%.2f}", 
                r.getMovie().getTitle(), 
                r.getDaysRented(), 
                r.getAmount()))
            .collect(Collectors.toList());
        
        return String.format(
            "{\"customer\":\"%s\",\"rentals\":[%s],\"totalAmount\":%.2f,\"frequentRenterPoints\":%d}",
            customer.getName(),
            String.join(",", rentalsJson),
            customer.getTotalAmount(),
            customer.getTotalFrequentRenterPoints());
    }
}